/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Enum.SearchGeneralEnum;
import dao.NguoiDungDAO;
import dao.SanPhamCuaHangDAO;
import dao.SanPhamDAO;
import java.awt.Component;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pojo.NguoiDung;
import pojo.SanPham;
import pojo.SanPhamCuaHang;

/**
 *
 * @author softviet
 */
public class DialogGeneralSeach extends javax.swing.JDialog {

    /**
     * Creates new form DialogGeneralSeach
     */
    private MyEventListener ButtonOKClickedListenner;
    SearchGeneralEnum searchGeneralEnum;
    private List<?> list;
    private static Logger logger = Logger.getLogger(DialogGeneralSeach.class);

    public DialogGeneralSeach(java.awt.Frame parent, boolean modal, SearchGeneralEnum searchGeneralEnum) throws Exception {
        super(parent, modal);
        initComponents();
        this.searchGeneralEnum = searchGeneralEnum;
        SetTabOrder();
        AutoLoad();
    }

    private void SetTabOrder() {
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy = new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtTen, btnTimKiem, btnChapNhan, btnThoat});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }

    private void AutoLoad() throws Exception {
        switch (searchGeneralEnum) {
            case KhachHang:
                this.setTitle("Tìm kiếm nhanh khách hàng");
                lblTen.setText("Tên khách hàng");
                list = NguoiDungDAO.TimKiemKhachHang(null, "", "",true);
                Utility.CreateColumnsFromDB(NguoiDung.class, list, tbDanhSach, new String[]{"Mã khách hàng", "Tên khách hàng", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh"}, new String[]{"username", "password", "tenLoaiNguoiDung", "tenCuaHang"});
                break;
            case SanPhamCuaHang:
                this.setTitle("Tìm kiếm nhanh sản phẩm cửa hàng");
                lblTen.setText("Tên sản phẩm");
                list = SanPhamCuaHangDAO.TimKiem(null, "", null, null, null, null, MainGUI.nguoiDung, true);
                Utility.CreateColumnsFromDB(SanPhamCuaHang.class, list, tbDanhSach, new String[]{"Mã sản phẩm cửa hàng", "Tên sản phẩm", "Tên cửa hàng", "Số lượng", "Loại sản phẩm", "Đơn giá"}, null);
                break;
            case SanPham:
                this.setTitle("Tìm kiếm nhanh sản phẩm");
                lblTen.setText("Tên sản phẩm");
                list = SanPhamDAO.TimKiem(null, "", null, null, null);
                Utility.CreateColumnsFromDB(SanPham.class, list, tbDanhSach, new String[]{"Mã sản phẩm", "Tên sản phẩm", "Loại sản phẩm", "Đơn giá"}, new String[]{"hinhAnh"});
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTen = new javax.swing.JLabel();
        txtTen = new javax.swing.JTextField();
        btnTimKiem = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSach = new org.jdesktop.swingx.JXTable();
        btnChapNhan = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        lblTen.setText("lblTen");

        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        tbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSach.setHorizontalScrollEnabled(true);
        tbDanhSach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSach);

        btnChapNhan.setText("Chấp nhận");
        btnChapNhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChapNhanActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblTen)
                        .addGap(18, 18, 18)
                        .addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnTimKiem))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(150, 150, 150)
                        .addComponent(btnChapNhan)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat)))
                .addContainerGap(99, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTen)
                    .addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTimKiem))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnChapNhan)
                    .addComponent(btnThoat))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        // TODO add your handling code here:
        try {
            switch (searchGeneralEnum) {
                case KhachHang:
                    list = NguoiDungDAO.TimKiemKhachHang(null, txtTen.getText().trim(), "",true);
                    Utility.CreateColumnsFromDB(NguoiDung.class, list, tbDanhSach, new String[]{"Mã khách hàng", "Tên khách hàng", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh"}, new String[]{"username", "password", "tenLoaiNguoiDung", "tenCuaHang"});
                    break;
                case SanPhamCuaHang:
                    list = SanPhamCuaHangDAO.TimKiem(null, txtTen.getText().trim(), null, null, null, null, MainGUI.nguoiDung, true);
                    Utility.CreateColumnsFromDB(SanPhamCuaHang.class, list, tbDanhSach, new String[]{"Mã sản phẩm cửa hàng", "Tên sản phẩm", "Tên cửa hàng", "Số lượng", "Loại sản phẩm", "Đơn giá"}, null);
                    break;
                case SanPham:
                    list = SanPhamDAO.TimKiem(null, txtTen.getText().trim(), null, null, null);
                    Utility.CreateColumnsFromDB(SanPham.class, list, tbDanhSach, new String[]{"Mã sản phẩm", "Tên sản phẩm", "Loại sản phẩm", "Đơn giá"}, new String[]{"hinhAnh"});
                    break;
            }
            if (list.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnChapNhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChapNhanActionPerformed
        // TODO add your handling code here:
        if (ButtonOKClickedListenner != null) {
            List<?> tempList = Utility.GetSelectedRows(tbDanhSach, list);
            Object object = null;
            if (!tempList.isEmpty()) {
                object = tempList.get(0);
            }
            ButtonOKClickedListenner.myEventOccured(object);
            dispose();
        }
    }//GEN-LAST:event_btnChapNhanActionPerformed

    private void tbDanhSachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            btnChapNhanActionPerformed(null);
        }
    }//GEN-LAST:event_tbDanhSachMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChapNhan;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblTen;
    private org.jdesktop.swingx.JXTable tbDanhSach;
    private javax.swing.JTextField txtTen;
    // End of variables declaration//GEN-END:variables

    /**
     * @param ButtonOKClickedListenner the ButtonOKClickedListenner to set
     */
    public void setButtonOKClickedListenner(MyEventListener ButtonOKClickedListenner) {
        this.ButtonOKClickedListenner = ButtonOKClickedListenner;
    }
}
